package com.felix.sim.spring.boot.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

public class SimOnClassCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Map<String, Object> attributes = metadata.getAnnotationAttributes(SimConditionOnClass.class.getName());
        String className = (String) attributes.get("value");
        try{
            context.getClassLoader().loadClass(className);
            return true;
        }catch (ClassNotFoundException ex){
            return false;
        }
    }
}
